package com.cqrcb.cloud.service;

import com.cqrcb.cloud.utils.FileUtil;
import com.cqrcb.cloud.utils.SshUtil;
import com.cqrcb.cloud.utils.SftpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.cqrcb.cloud.properties.*;
import org.springframework.util.CollectionUtils;

/**
 * @author ：29520
 * @date ：Created in 2021/8/4 15:10
 * @description：
 * @modified By：
 * @version:
 */
@Slf4j
@Component
public class SftpService {

    @Autowired
    private SftpProperties sftpProperties;

    private static String split = ",";
    private static String type_sh = "sh";
    private static String type_upload = "upload";

    @PostConstruct
    private void init() throws Exception {
//        log.info("=====================任务开始=====================");
//        log.info("\n");
//        cmd();
//        log.info("\n");
//        log.info("=====================任务结束=====================");

        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        java.io.File file = applicationHome.getDir();
        String route = file.getAbsolutePath();
        log.info("======{}",route);
    }

    private void cmd() throws Exception{
        SshUtil sshService = new SshUtil();
        List<SftpServerProperties> servers = sftpProperties.getServers();
        log.info("sftpProperties->{}" , sftpProperties);
        for(SftpServerProperties server : servers){
            List<String> operates = server.getOperates();
            for(String operate : operates){
                log.info("\n");
                log.info("=====================分割线=====================");
                log.info("操作 {}" , operate);
                String[] split = operate.split(SftpService.split);
                String type = split[0];
                if(type_sh.equals(type)){
                    int waitMax = Integer.parseInt(split[1]);
                    String sh = split[2];
                    execSh(sshService, server, sh,waitMax);
                }else if(type_upload.equals(type)){
                    String localFile = split[1];
                    String remoteFile = split[2];
                    File file = new File(localFile);
                    //如果是目录
                    if(file.isDirectory()){
                        uploadPath(server, localFile, remoteFile, file);
                    }else{
                        uploadFile(server, localFile, remoteFile);
                    }
                }
            }
        }
    }
    private void execSh(SshUtil sshService, SftpServerProperties server, String sh,int waitMax) throws IOException {
        StringBuilder exec = sshService.exec(server, sh,waitMax);
        log.info("正在执行命令->");
        log.info("{}",sh);

        log.info("执行结果->",sh);
        log.info("{}",exec);
    }

    private void uploadFile(SftpServerProperties server, String localFile, String remoteFile) throws Exception {
        log.info("正在上传本地文件 {},  远程目录 {}",localFile ,remoteFile );
        SftpUtil sftpUtil = new SftpUtil(server);
        sftpUtil.upload(new File(localFile), remoteFile);
        sftpUtil.closeChannel();
    }

    private void uploadPath(SftpServerProperties server, String localFile, String remoteFile, File file) throws Exception {
        ArrayList<String> files = new ArrayList<>();
        FileUtil.search(files,file);
        if(CollectionUtils.isEmpty(files)){
            return;
        }
        for(String f:files){
            File file1 = new File(f);
            String localPath = file1.getParent();
            localPath=localPath.replaceAll("\\\\", "/");
            String remoteFileSuffix = localPath.replaceAll(localFile, "").replaceAll("\\\\", "/");
            uploadFile(server,f, remoteFile + remoteFileSuffix);
        }
    }
}
